// This file demonstrates the different options of the pagination plugin
// It also demonstrates how to use a JavaScript data structure to
// generate the paginated content and how to display more than one
// item per page with items_per_page.
/**
* Callback function that displays the content.
*
* Gets called every time the user clicks on a pagination link.
*
* @param {int}page_index New Page index
* @param {jQuery} jq the container with the pagination links as a jQuery object
*/
function pageselectCallback(page_index   , jq ){
	// Get number of elements per pagionation page from form
	var items_per_page = $('#items_per_page').val();
	//var items_per_page = 3;
	var max_elem = Math.min((page_index+1) * items_per_page, data.length);
	var newcontent = '';
	// Iterate through a selection of the content and build an HTML string
	
	for(var i=page_index*items_per_page;i<max_elem;i++)
	{
		/*newcontent += '<dt>' + members[i][0] + '</dt>';
		newcontent += '<dd class="state">' + members[i][2] + '</dd>';
		newcontent += '<dd class="party">' + members[i][3] + '</dd>';*/
		
		/*newcontent += '<dt>' + data[i].Product.name + '</dt>';
		newcontent += '<dd class="state">' + "aa" + '</dd>';
		newcontent += '<dd class="party">' + "ss" + '</dd>';*/
			var tur = data[i].Product.category;
			
			if(tur == 'Kitap'){
				tur = 'books';
			} else if (tur == 'Müzik'){
				tur = 'musics';
			} else if (tur == 'Film') {
				tur = 'films';
			}
		
			newcontent +=	'<div class="product">';
			newcontent +=	'<div class="product_name">';	
			newcontent +=	'<a href="/bookstore/' + tur + '/view/' + data[i].Product.id + '">' + data[i].Product.name + '</a>';	
			newcontent +=	'</div>';	
			newcontent +=	'<div class="urunresim">';	
			newcontent +=	'<a href="/bookstore/' + tur + '/view/' + data[i].Product.id + '">';	
			newcontent +=	'<img class="urunresim" alt="' + data[i].Product.name + '" src="/bookstore/img/' + data[i].Product.image + '">';	
			newcontent +=	'</a>';	
			newcontent +=	'</div>';	
			newcontent +=	'<div class="producer">' + data[i].Producer.fname + ' ' + data[i].Producer.lname +'</div>';	
			newcontent +=	'<div class="price">' + data[i].Product.price + ' TL</div>';	
			newcontent +=	'<button class="add2cart button pill" onclick="add2cart(' + data[i].Product.id + ')" type="submit"><span class="rightarrow icon"></span>Sepete Ekle</button>';	
			newcontent +=	'</div>';
			
		
	}
	// Replace old content with new content
	$('#products_container').html(newcontent);
	// Prevent click eventpropagation
	return false;
}
// The form contains fields for many pagiantion optiosn so you can
// quickly see the resuluts of the different options.
// This function creates an option object for the pagination function.
// This will be be unnecessary in your application where you just set
// the options once.
function getOptionsFromForm(){
	var opt = {callback: pageselectCallback};
	// Collect options from the text fields - the fields are named like their option counterparts
	$("input:text").each(function(){
	opt[this.name] = this.className.match(/numeric/) ? parseInt(this.value) : this.value;
	});
	
	// Avoid html injections in this demo
	var htmlspecialchars ={ "&":"&amp;", "<":"&lt;", ">":"&gt;", '"':"&quot;"};
	$.each(htmlspecialchars, function(k,v){
		//opt.prev_text = opt.prev_text.replace(k,v);
		//opt.next_text = opt.next_text.replace(k,v);
		opt.prev_text = "Önceki";
		opt.next_text = "Sonraki";
	});
	return opt;
}
// When document has loaded, initialize pagination and form
$(document).ready(function(){
	// Create pagination element with options from form
	
	var optInit = getOptionsFromForm();
	$("#Pagination").pagination(data.length, optInit);
	
	// Event Handler for for button
	$("#setoptions").click(function(){
		var opt = getOptionsFromForm();
		// Re-create pagination content with new parameters
		$("#Pagination").pagination(data.length, opt);
	});
}); 